package com.zhilun;

import cn.hutool.http.HttpUtil;
import cn.hutool.http.HttpResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhilun.check.entity.ZhiLunPositionDataParam;
import com.zhilun.check.entity.ZhiLunSensorDataParam;
import org.apache.commons.collections4.CollectionUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/*SELECT
        CONCAT(last.license_plate, '-', last.sensor_id, '-', last.location) AS sensor_id
        FROM
        device_bind_sensor_last last
        JOIN device_sensor sensor ON last.sensor_id = sensor.sensor_id
        WHERE
        sensor.manufacturer_id = 7 and last.license_plate !=''  and  last.device_id != ''*/
public class CheckZhilunSensor {
    public static void main(String[] args) throws IOException {
        List<String> zhiLun = new ArrayList<>();

        String url = "https://motorcade.zhilun.com/smartApi/motorcade-api/sensor/list?pageSize=30000&carrierBindStatus=1&companyId=30457";
        String filePath = "C:\\Users\\hcf\\IdeaProjects\\dache-test\\service-hi\\src\\main\\resources\\sensor-bind\\zhilun.txt"; // 文件路径

        List<String> daChe = Files.readAllLines(Paths.get(filePath));



        // 设置请求头
        HttpResponse response = HttpUtil.createGet(url)
                .header("Cookie", "acw_tc=0b32807617304384051301111e373d553695b404206ddf7ddb14c8316e319a")
                .header("Pragma", "no-cache")
                .header("Referer", "https://motorcade.zhilun.com/server/stock/tpms")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-origin")
                .header("access_token", "5d02dec6-704a-4098-8561-d4be358234ba")
                .header("role_type", "3")
                .header("sec-ch-ua", "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("system_version", "3")
                .execute();

        // 获取响应内容
        String responseBody = response.body();

        JSONObject jsonObject =JSONObject.parseObject(responseBody);
        JSONArray dataArray = jsonObject.getJSONArray("data");

        // 创建映射关系，反转键和值
        Map<String, Integer> translationLocation = new HashMap<>();
        translationLocation.put("L0102", 1);
        translationLocation.put("L0101", 2);
        translationLocation.put("R0101", 3);
        translationLocation.put("R0102", 4);
        translationLocation.put("L0202", 5);
        translationLocation.put("L0201", 6);
        translationLocation.put("R0201", 7);
        translationLocation.put("R0202", 8);
        translationLocation.put("L0302", 9);
        translationLocation.put("L0301", 10);
        translationLocation.put("R0301", 11);
        translationLocation.put("R0302", 12);
        translationLocation.put("L0003", 97);
        translationLocation.put("L0001", 98);
        translationLocation.put("L0002", 99);

        // 遍历数据并提取需要的信息
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject item = dataArray.getJSONObject(i);

            String carrierNo = item.getString("carrierNo");
            String sensorNo = item.getString("sensorNo");
            String position = item.getString("position");

            // 使用反转后的映射关系查找对应的 key
            Integer positionKey = translationLocation.get(position);

            if (positionKey != null) {
                String result = carrierNo + "-" + sensorNo + "-" + positionKey;
                zhiLun.add(result);
            }
        }

        List<ZhiLunSensorDataParam> list = new ArrayList<>();

        Collection<String> subtractList = CollectionUtils.subtract(zhiLun, daChe);
        Collection<String> subtractList2 = CollectionUtils.subtract(daChe, zhiLun);

        for (String s : subtractList) {
            String[] split = s.split("-");
            ZhiLunSensorDataParam zhiLunPositionDataParam = new ZhiLunSensorDataParam();
            zhiLunPositionDataParam.setLicensePlate(split[0]);
            zhiLunPositionDataParam.setSensorId(split[1]);
            zhiLunPositionDataParam.setLocation(split[2]);
            zhiLunPositionDataParam.setSource(null);
            list.add(zhiLunPositionDataParam);
        }

        // 填充 list2
        for (String s : subtractList2) {
            String[] split = s.split("-");
            ZhiLunSensorDataParam zhiLunPositionDataParam = new ZhiLunSensorDataParam();
            zhiLunPositionDataParam.setLicensePlate(split[0]);
            zhiLunPositionDataParam.setSensorId(split[1]);
            zhiLunPositionDataParam.setLocation(split[2]);
            zhiLunPositionDataParam.setSource(null);
            list.add(zhiLunPositionDataParam);
        }
        exportToExcel(list,"传感器.xlsx");
        //知轮有 大车无
        System.out.println("知轮有 大车无:");
        System.out.println(subtractList);
        //知轮无 大车有
        System.out.println("知轮无 大车有:");
        System.out.println(subtractList2);

        System.out.println(1);
    }

    public static void exportToExcel(List<ZhiLunSensorDataParam> data, String fileName) {
        EasyExcel.write(fileName, ZhiLunSensorDataParam.class)  // 写入到指定的文件，指定 ZhiLunPositionDataParam 类
                .sheet("Sheet1")                // 表格的名称
                .doWrite(data);                 // 写入数据
    }
}
